package com.custom.customloadingview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class CustomLoadingView extends View {

    private static final int mCount = 10;  //加载圆总数
    private static final int mRotateValue = 360 / mCount;  //每个加载圆圆心间隔角度差
    private float[] mAllRadius = new float[mCount];  //记录所有小圆半径
    private int[] mAllColors = new int[mCount]; //记录所有小圆颜色
    private float mMaxRadius;   //小圆最大半径
    private int mAnimateValue = 0;
    private Paint mPaint; //画笔
    private int mSize;
    private int mColor;
    private long mDuration;
    private ValueAnimator mAnimator = null;

    public CustomLoadingView(Context context) {
        super(context);
    }

    public CustomLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
        initPaint();//初始化画笔
        initValue();
    }

    public CustomLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomLoadingView);

        mSize = (int) typedArray.getDimension(R.styleable.CustomLoadingView_custom_size, mSize);
        mColor = typedArray.getColor(R.styleable.CustomLoadingView_custom_color, mColor);
        mDuration = typedArray.getInt(R.styleable.CustomLoadingView_custom_duration, 1000);
        typedArray.recycle();//回收
        Log.e("message", String.valueOf(mSize));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mSize, mSize);
    }

    /**
     * 初始化所有加载圆半径、颜色 颜色绘制可改变透明度
     */
    private void initValue() {
        float mMinRadius = mSize / 20;
        Log.e("minCircleRadius", String.valueOf(mMinRadius));
        for (int i = 0; i < mCount; i++) {//循环从0开始 所以降位
            switch (i) {
                /**
                 * 圆1 2 3 4 5 大小透明度一致
                 * 圆6 7 大小透明度增加
                 * 圆9大小透明度最大
                 * 圆10 8 大小一致透明度一致 倒数第二
                 */
                case 9:
                    mAllRadius[i] = mMinRadius * 2f;
                    mAllColors[i] = (int) (255);
                    break;
                case 8:
                case 10:
                    mAllRadius[i] = mMinRadius * 1.75f;
                    mAllColors[i] = (int) (255 * 0.9f);
                    break;
                case 7:
                    mAllRadius[i] = mMinRadius * 1.5f;
                    mAllColors[i] = (int) (255 * 0.8f);
                    break;
                case 6:
                    mAllRadius[i] = mMinRadius * 1.25f;
                    mAllColors[i] = (int) (255 * 0.7f);
                    break;
                default:
                    mAllRadius[i] = mMinRadius;
                    mAllColors[i] = (int) (255 * 0.5f);
                    break;
            }
        }
        mMaxRadius = mMinRadius * 2;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (mSize > 0) {
            //mRotateValue * mAnimateValue角度，绘制所有小圆
            canvas.rotate(mRotateValue * mAnimateValue, mSize / 2, mSize / 2);//以中心点旋转 直到整体旋转360度
            for (int i = 0; i < mCount; i++) {
                //设置加载圆透明度
                mPaint.setAlpha(mAllColors[i]);
                //每隔mRotateValue角度，绘制一个加载圆
                canvas.drawCircle(mSize / 2, mMaxRadius, mAllRadius[i], mPaint);//绘制加载圆
                canvas.rotate(mRotateValue, mSize / 2, mSize / 2);//旋转加载圆圆心间隔角度差
            }
        }
    }

    /**
     * 开始动画
     */
    public void start() {
        if (mAnimator == null) {//关闭后还能运行
            mAnimator = ValueAnimator.ofInt(0, mCount - 1);
            mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mAnimateValue = (int) animation.getAnimatedValue();
                    postInvalidate();//重绘刷新整个View
                }
            });

            mAnimator.setDuration(mDuration);
            mAnimator.setRepeatMode(ValueAnimator.RESTART);
            mAnimator.setRepeatCount(ValueAnimator.INFINITE);
            mAnimator.start();
        } else if (!mAnimator.isStarted()) {
            mAnimator.start();
        }

    }


    /**
     * 关闭动画
     */
    public void stop() {
        if (mAnimator != null) {
            mAnimator.removeAllUpdateListeners();
            mAnimator.cancel();
            mAnimator = null;//设置关闭状态
        }
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mColor);
    }

}
